package com.anche.cywebnew.Controller.configManager;

import com.anche.cywebnew.DAO.configManager.SysOptionMapper;
import com.anche.cywebnew.Pojo.configManager.SysOptions;
import com.anche.cywebnew.Service.configManager.SysOptionService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/config/sysoption")
public class SysOptionController {

    @Autowired
    private SysOptionService sysOptionService;
    @Autowired
    private SysOptionMapper sysOptionMapper;

    private static final Logger logger = LoggerFactory.getLogger(SysOptionController.class);


    @RequestMapping("/manager")
    public String toSysOptionPage() {
        return "configManager/sysoption";
    }

    @GetMapping("/sysOptionPager")
    @ResponseBody
    public Object getSysOptionPager(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, SysOptions sysOptions) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<SysOptions> sysOptionPager = sysOptionService.getSysOptionPager(page, limit, sysOptions);
            logger.info("<查询系统配置列表成功>");
            map.put("code", "0");
            map.put("count", sysOptionPager.getTotal());
            map.put("msg", "查询成功");
            map.put("data", sysOptionPager.getResult());
        } catch (Exception e) {
            logger.error("<查询系统配置列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            map.put("code", "1");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @GetMapping("/list")
    @ResponseBody
    public Object getSysOptionList(SysOptions sysOptions) {
        try {
            List<SysOptions> sysOptionList = sysOptionService.getSysOptionList(sysOptions);
            logger.info("<查询系统配置列表成功>");
            return ResultUtil.success(sysOptionList);
        } catch (Exception e) {
            logger.error("<查询系统配置列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @GetMapping("/optionKindList")
    @ResponseBody
    public Object getOptionKindList() {
        try {
            List<String> optionKindList = sysOptionService.getOptionKindList();
            logger.info("<获取配置类别列表成功>");
            return ResultUtil.success(optionKindList);
        } catch (Exception e) {
            logger.error("<获取配置类别列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/optionInfo", method = RequestMethod.POST)
    @ResponseBody
    @OptLog(title = "添加功能配置信息", type = OptType.ADD)
    public Object addSysOption(SysOptions sysOptions) {
        try {
            sysOptionService.addSysOption(sysOptions);
            logger.info("<新增[{}]功能配置成功>", sysOptions.getOption_params());
            return ResultUtil.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<新增[{}]功能配置异常>:{}", sysOptions.getOption_params(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/optionInfo", method = RequestMethod.PUT)
    @ResponseBody
    @OptLog(title = "更新功能配置信息", type = OptType.UPDATE)
    public Object setSysOption(SysOptions sysOptions) {
        try {
            sysOptionService.setSysOption(sysOptions);
            logger.info("<更新[{}]功能配置成功>", sysOptions.getOption_params());
            return ResultUtil.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新[{}]功能配置异常>:{}", sysOptions.getOption_params(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }


    @RequestMapping(value = "/optionInfo", method = RequestMethod.DELETE)
    @ResponseBody
    @OptLog(title = "删除功能配置信息", type = OptType.DEL)
    public Object delOption(@RequestParam("id") String id) {
        try {
            sysOptionService.delSysOption(id);
            return ResultUtil.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<删除id为 " + id + " 的功能配置异常>:{}", e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

}
